package cn.org.donald.framework.controller;

import cn.org.donald.framework.service.SysDictionaryService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import cn.org.donald.framework.constant.InfoConstant;
import cn.org.donald.framework.pojo.vo.HttpResult;
import cn.org.donald.framework.pojo.vo.TableResult;
import cn.org.donald.pojo.entity.Dictionary;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.Comparator;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author Donald
 * @since 2020-11-11
 */
@RestController
@RequestMapping("/sysDictionary")
@Api(value = "数据字典",description = "数据字典相关接口")
public class SysDictionaryController extends BaseController {

    @Autowired
    private SysDictionaryService sysDictionaryService;

    @GetMapping("/getByParentCode")
    @ApiOperation(value = "根据父级字典获取字典组")
    public TableResult getByParentCode(String parentCode){
        QueryWrapper<Dictionary> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("parent_code",parentCode);
        List<Dictionary> list = sysDictionaryService.list(queryWrapper);
        list.sort(Comparator.comparing(Dictionary::getSort));
        return toTableResult(list);
    }

    @PostMapping("/getParentList")
    @ApiOperation(value = "获取数据字典分组父集合")
    @RequiresPermissions("sysDictionary:getParentList:post")
    public TableResult getParentList(){
        QueryWrapper<Dictionary> queryWrapper = new QueryWrapper<>();
        queryWrapper.isNull("parent_code");
        return toTableResult(sysDictionaryService.list(queryWrapper));
    }

    @PostMapping("/add")
    @ApiOperation(value = "添加字典")
    @RequiresPermissions("sysDictionary:add:post")
    public HttpResult add(@RequestBody Dictionary dictionary){
        if(!sysDictionaryService.isExistByCode(dictionary.getCode())){
            return toResultFail("code已存在!");
        }
        return sysDictionaryService.save(dictionary)?toResultSuccess(InfoConstant.ADD_SUCCESS) : toResultFail(InfoConstant.ADD_FAIL);
    }

    @PutMapping("/edit")
    @ApiOperation(value = "修改字典")
    @RequiresPermissions("sysDictionary:edit:put")
    public HttpResult edit(@RequestBody Dictionary dictionary){
        if (sysDictionaryService.isExistByCode(dictionary.getCode())){
            return toResultFail("code已存在");
        }
        return sysDictionaryService.updateById(dictionary)? toResultSuccess(InfoConstant.EDIT_SUCCESS) : toResultFail(InfoConstant.EDIT_FAIL);
    }

    @DeleteMapping("/deleteAndChild/{id}")
    @ApiOperation(value = "删除字典属性或分组")
    @RequiresPermissions("sysDictionary:deleteAndChild:put")
    public HttpResult deleteAndChild(@PathVariable Long id){
        return sysDictionaryService.removeAndChild(id) ? toResultSuccess(InfoConstant.DELETE_SUCCESS) : toResultFail(InfoConstant.DELETE_FAIL);
    }
}

